using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Forecast.Database;

namespace Forecast.Business
{
    public class PredictionResults
    {
        #region Enums

        public enum Columns
        {
            None,
            PlayerId,
            ActivityLevel,
            Rank,
            Name, 
            Result
        }

        #endregion  
        #region Interface

        internal DataTable GetPredictionResults(Prediction prediction, IList<Player> allPlayers, IList<PredictionResult> predictionResults)
        {
            DataTable table = GetEmptyDataTableForPredictionResults();

            int rank = 0;
            foreach (PredictionResult predictionResult in predictionResults)
            {
                rank++;
                DataRow row = table.NewRow();

                row[Columns.PlayerId.ToString()] = predictionResult.PlayerId;
                row[Columns.Name.ToString()] = GetPlayerName(predictionResult.PlayerId, allPlayers);
                row[Columns.ActivityLevel.ToString()] = predictionResult.NumTrades;
                row[Columns.Rank.ToString()] = rank;
                row[Columns.Result.ToString()] = predictionResult.CashEarned;

                table.Rows.Add(row);
            }

            return table;
        }

        #endregion
        #region Helper methods

        private string GetPlayerName(int playerId, IList<Player> allPlayers)
        {
            string playerName = "Not found";

            foreach (Player player in allPlayers)
            {
                if (player.Id == playerId)
                    return player.UserName;
            }

            return playerName;
        }

        private DataTable GetEmptyDataTableForPredictionResults()
        {
            DataTable dataTable = new DataTable("PlayerOverview");

            DataColumn columnPlayerId = new DataColumn();
            columnPlayerId.ColumnName = Columns.PlayerId.ToString();
            columnPlayerId.DataType = typeof(int);
            dataTable.Columns.Add(columnPlayerId);

            DataColumn columnPlayerName = new DataColumn();
            columnPlayerName.ColumnName = Columns.Name.ToString();
            columnPlayerName.DataType = typeof(string);
            dataTable.Columns.Add(columnPlayerName);

            DataColumn columnActivityLevel = new DataColumn();
            columnActivityLevel.ColumnName = Columns.ActivityLevel.ToString();
            columnActivityLevel.DataType = typeof(int);
            dataTable.Columns.Add(columnActivityLevel);

            DataColumn columnPosition = new DataColumn();
            columnPosition.ColumnName = Columns.Rank.ToString();
            columnPosition.DataType = typeof(int);
            dataTable.Columns.Add(columnPosition);

            DataColumn columnResult = new DataColumn();
            columnResult.ColumnName = Columns.Result.ToString();
            columnResult.DataType = typeof(int);
            dataTable.Columns.Add(columnResult);

            return dataTable;
        }

        #endregion
    }
}
